<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
use alipaysdk\aop\Tmdsx;
class Order extends Base{
	// 下单
	public function orderIn()
	{
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
		if(!empty(input("param.p_id"))){
			$data['p_id']=input("param.p_id");
		}else{
			return json(['code' => 502, 'msg' => '产品id']);
		}
		if(!empty(input("param.buy_num"))){
			$data['buy_num']=input("param.buy_num");
		}else{
			return json(['code' => 503, 'msg' => '购买数量']);
		}
		if(!empty(input("param.price"))){
			$data['price']=input("param.price");
		}else{
			return json(['code' => 504, 'msg' => '单价']);
		}
		if(!empty(input("param.pay_fee"))){
			$data['pay_fee']=input("param.pay_fee");
			$data['f_pay_fee']=input("param.pay_fee");
		}else{
			return json(['code' => 505, 'msg' => '支付金额']);
		}
		if(!empty(input("param.name"))){
			$data['name']=input("param.name");
		}else{
			return json(['code' => 506, 'msg' => '收货人']);
		}
		if(!empty(input("param.phone"))){
			$data['phone']=input("param.phone");
		}else{
			return json(['code' => 507, 'msg' => '收货电话']);
		}
		if(!empty(input("param.address"))){
			$data['address']=input("param.address");
		}else{
			return json(['code' => 508, 'msg' => '收货地址']);
		}
		if(!empty(input("param.spec_combination_id"))){
			$data['spec_combination_id']=input("param.spec_combination_id");
		}else{
			return json(['code' => 509, 'msg' => '参数id']);
		}
		if(!empty(input("param.combination"))){
			$data['combination']=input("param.combination");
		}else{
			return json(['code' => 510, 'msg' => '类别参数']);
		}
		$data['message']=input("param.message");
		if(input("param.pay_style")==1||input("param.pay_style")==2){
			$data['pay_style']=input("param.pay_style");
		}else{
			return json(['code' => 512, 'msg' => '支付方式']);
		}
        $uid=UserCrypt::do_mdecrypt($token);
		if(!empty(input("param.express_style"))){
			if(input("param.express_style")==3){
				$isHad=Db::name('ordersub')->where('user_id',$uid)->where('state','in','2,3,4,5')->find();
				if($isHad){
					return json(['code' => 513, 'msg' => '到货付款只限首次']);
				}
			}
			$data['express_style']=input("param.express_style");
		}else{
			return json(['code' => 513, 'msg' => '配送方式']);
		}
		if(!empty(input("param.express_fee"))){
			$data['express_fee']=input("param.express_fee");
		}else{
			return json(['code' => 514, 'msg' => '快递费']);
		}
		if(!empty(input("param.address_id"))){
			$data['address_id']=input("param.address_id");
		}else{
			return json(['code' => 515, 'msg' => '默认收货地址id']);
		}
		//发票...
		if(input("param.is_invoice")==1||input("param.is_invoice")==2){
			$data['is_invoice']=input("param.is_invoice");
			if($data['is_invoice']==2){
				if(input("param.company_name")){
					$data['company_name']=input("param.company_name");
				}
				if(input("param.invoice_no")){
					$data['invoice_no']=input("param.invoice_no");
				}
				if(input("param.invoice_address")){
					$data['invoice_address']=input("param.invoice_address");
				}
				if(input("param.invoice_phone")){
					$data['invoice_phone']=input("param.invoice_phone");
				}
				if(input("param.invoice_bank")){
					$data['invoice_bank']=input("param.invoice_bank");
				}
				if(input("param.invoice_bank_no")){
					$data['invoice_bank_no']=input("param.invoice_bank_no");
				}
			}
		}else{
			return json(['code' => 516, 'msg' => '是否开发票']);
		}
		$data['user_id']=$uid;
		$data['orderno']=date('YmdHis',time()).mt_rand(1111,9999);
		$data['score']=input("param.score");
		$data['state']=1;
		$data['is_cart']=1;
		$data['sub']=1;
		$data['time']=time();
		$data['uptime']=time();

		Db::startTrans();
        try {
			$orderId=Db::name('orders')->insertGetId($data);
			$datasub['user_id']=$data['user_id'];
			$datasub['orders_id']=$orderId;
			$datasub['ordernosub']=$data['orderno'].'1';
			$datasub['cart_id']=0;
			$datasub['p_id']=$data['p_id'];
			$datasub['title']=Db::name('product')->where('id',$data['p_id'])->value('title');
			$datasub['buy_num']=$data['buy_num'];
			$datasub['price']=$data['price'];
			$datasub['spec_combination_id']=$data['spec_combination_id'];
			$datasub['combination']=$data['combination'];
			$datasub['pay_fee']=$data['pay_fee'];
			$datasub['f_pay_fee']=$datasub['pay_fee'];
			$datasub['message']=$data['message'];
			$datasub['express_style']=$data['express_style'];
			$datasub['is_invoice']=$data['is_invoice'];
			$datasub['company_name']=input("param.company_name");
			$datasub['invoice_no']=input("param.invoice_no");
			$datasub['invoice_address']=input("param.invoice_address");
			$datasub['invoice_phone']=input("param.invoice_phone");
			$datasub['invoice_bank']=input("param.invoice_bank");
			$datasub['invoice_bank_no']=input("param.invoice_bank_no");
			$datasub['pay_style']=input("param.pay_style");
			$datasub['score']=$data['score'];
			$datasub['state']=1;
			$datasub['time']=time();
			$datasub['uptime']=time();
			Db::name('ordersub')->insert($datasub);
            Db::name('spec_combination')->where('id',$data['spec_combination_id'])->setDec('num',$data['buy_num']);
			//express_style为3不用支付
			// 提交事务
            Db::commit();
            return json(['code' => 200, 'msg' => '成功！']);
		} catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 500, 'msg' => '事物回滚失败！']);
        }
	}

	public function corderIn()
	{
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.car_ids"))){
            $car_ids=json_decode(input("param.car_ids"));
        }else{
            return json(['code' => 502, 'msg' => '数组呢']);
        }
		if(!empty(input("param.pay_fee"))){
			$data['pay_fee']=input("param.pay_fee");
			$data['f_pay_fee']=$data['pay_fee'];
		}else{
			return json(['code' => 505, 'msg' => '支付金额']);
		}
		if(!empty(input("param.name"))){
			$data['name']=input("param.name");
		}else{
			return json(['code' => 506, 'msg' => '收货人']);
		}
		if(!empty(input("param.phone"))){
			$data['phone']=input("param.phone");
		}else{
			return json(['code' => 507, 'msg' => '收货电话']);
		}
		if(!empty(input("param.address"))){
			$data['address']=input("param.address");
		}else{
			return json(['code' => 508, 'msg' => '收货地址']);
		}
		$data['message']=input("param.message");
        $uid=UserCrypt::do_mdecrypt($token);
		if(input("param.pay_style")==1||input("param.pay_style")==2){
			if(input("param.express_style")==3){
				$isHad=Db::name('ordersub')->where('user_id',$uid)->where('state','in','2,3,4,5')->find();
				if($isHad){
					return json(['code' => 513, 'msg' => '到货付款只限首次']);
				}
			}
			$data['pay_style']=input("param.pay_style");
		}else{
			return json(['code' => 512, 'msg' => '支付方式']);
		}
		if(!empty(input("param.express_style"))){
			$data['express_style']=input("param.express_style");
		}else{
			return json(['code' => 513, 'msg' => '配送方式']);
		}
		if(!empty(input("param.express_fee"))){
			$data['express_fee']=input("param.express_fee");
		}else{
			return json(['code' => 514, 'msg' => '快递费']);
		}
		if(!empty(input("param.address_id"))){
			$data['address_id']=input("param.address_id");
		}else{
			return json(['code' => 515, 'msg' => '默认收货地址id']);
		}
		//发票...
		if(input("param.is_invoice")==1||input("param.is_invoice")==2){
			$data['is_invoice']=input("param.is_invoice");
			if($data['is_invoice']==2){
				if(input("param.company_name")){
					$data['company_name']=input("param.company_name");
				}
				if(input("param.invoice_no")){
					$data['invoice_no']=input("param.invoice_no");
				}
				if(input("param.invoice_address")){
					$data['invoice_address']=input("param.invoice_address");
				}
				if(input("param.invoice_phone")){
					$data['invoice_phone']=input("param.invoice_phone");
				}
				if(input("param.invoice_bank")){
					$data['invoice_bank']=input("param.invoice_bank");
				}
				if(input("param.invoice_bank_no")){
					$data['invoice_bank_no']=input("param.invoice_bank_no");
				}
			}
		}else{
			return json(['code' => 516, 'msg' => '是否开发票']);
		}
		$data['user_id']=$uid;
      
		$body=$car_ids[0]->title;
        $data['p_id']=$car_ids[0]->p_id;
        $data['buy_num']=$car_ids[0]->buy_num;
        $data['price']=$car_ids[0]->money;
        $data['spec_combination_id']=$car_ids[0]->spec_combination_id;
        $data['combination']=$car_ids[0]->combination;
		$data['orderno']=date('YmdHis',time()).mt_rand(1111,9999);
		$data['score']=input("param.score");
		$data['state']=1;
		$data['is_cart']=2;
        if(count($car_ids)>1){
            $data['sub']=2;
        }elseif(count($car_ids)==1){
            $data['sub']=1;
        }
		$data['time']=time();
		$data['uptime']=time();

		Db::startTrans();
        try {
			$orderId=Db::name('orders')->insertGetId($data);
			foreach ($car_ids as $key => $value) {

				$datasub['user_id']=$data['user_id'];
				$datasub['orders_id']=$orderId;
                $datasub['ordernosub']=$data['orderno'].$key;
                $datasub['cart_id']=$car_ids[$key]->id;
                $datasub['p_id']=$car_ids[$key]->p_id;
				$datasub['title']=$car_ids[$key]->title;
                $datasub['buy_num']=$car_ids[$key]->buy_num;
                $datasub['price']=$car_ids[$key]->money;
                $datasub['spec_combination_id']=$car_ids[$key]->spec_combination_id;
                $datasub['combination']=$car_ids[$key]->combination;
                $datasub['pay_fee']=bcmul($datasub['buy_num'],$datasub['price'],2);
				$datasub['f_pay_fee']=$datasub['pay_fee'];
				$datasub['message']=$data['message'];
				$datasub['express_style']=$data['express_style'];
				$datasub['is_invoice']=$data['is_invoice'];
				$datasub['company_name']=input("param.company_name");
				$datasub['invoice_no']=input("param.invoice_no");
				$datasub['invoice_address']=input("param.invoice_address");
				$datasub['invoice_phone']=input("param.invoice_phone");
				$datasub['invoice_bank']=input("param.invoice_bank");
				$datasub['invoice_bank_no']=input("param.invoice_bank_no");
				$datasub['pay_style']=input("param.pay_style");
				// $datasub['score']=$data['score'];
				$datasub['state']=1;
				$datasub['time']=time();
				$datasub['uptime']=time();
				Db::name('ordersub')->insert($datasub);
                Db::name('carts')->where('id',$car_ids[$key]->id)->update(['state'=>3,'uptime'=>time()]);
                Db::name('spec_combination')->where('id',$car_ids[$key]->spec_combination_id)->setDec('num',$car_ids[$key]->buy_num);
            }
            //express_style==3到货付款
			// 提交事务
            Db::commit();
            return json(['code' => 200, 'msg' => '成功！']);
		} catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 500, 'msg' => '事物回滚失败！']);
        }
	}
	// 我的订单
	public function myOrders()
	{
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
      	
        $keyword=input('param.keyword');
        $map = [
        ];
        if(input("param.state")==1||input("param.state")==2||input("param.state")==3||input("param.state")==4||input("param.state")==5||input("param.state")==6){
            array_push($map,['state','=',input("param.state")]);
        }else{
            array_push($map,['state','<',7]);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('ordersub')->where($map)->where('user_id',$uid)->where('title|ordernosub','like',"%{$keyword}%")->order('id desc')->limit(10*($page-1),10)->select();
        if ($res) {
        	foreach ($res as $k => $v) {
        		$res[$k]['time']=date('Y-m-d H:i:s',$res[$k]['time']);
                $res[$k]['img']=Db::name('product')->where('id',$res[$k]['p_id'])->value('img');
        	}
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
		}else{
            return json(['code' => 500, 'msg' => '查无数据！']);
		}
	}
	public function myOrderses()
	{
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
      	
        $keyword=input('param.keyword');
        $map = [
        ];
        if(input("param.state")==1||input("param.state")==2||input("param.state")==3||input("param.state")==4||input("param.state")==5||input("param.state")==6){
            array_push($map,['state','=',input("param.state")]);
        }else{
            array_push($map,['state','<',7]);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('orders')->where($map)->where('user_id',$uid)->where('orderno','like',"%{$keyword}%")->order('id desc')->limit(10*($page-1),10)->select();
        if ($res) {
        	foreach ($res as $k => $v) {
        		// $res[$k]['ordersubarr']=Db::name('ordersub')->where('orders_id',$res[$k]['id'])->where('user_id',$uid)->order('id desc')->select();
        		$res[$k]['ordersubarr']=Db::name('ordersub')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->field('a.*,b.img')->where('a.orders_id',$res[$k]['id'])->where('a.user_id',$uid)->order('a.id desc')->select();
        		$res[$k]['time']=date('Y-m-d H:i:s',$res[$k]['time']);
                $res[$k]['img']=Db::name('product')->where('id',$res[$k]['p_id'])->value('img');
        	}
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
		}else{
            return json(['code' => 500, 'msg' => '查无数据！']);
		}
	}
	// 取消订单
	public function cancel()
	{
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => 'id呢？']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('ordersub')->where(array('user_id'=>$uid,'id'=>$id,'state'=>1))->update(['state'=>6]);
        if($res){
            return json(['code' => 200, 'msg' => '成功！']);
        }else{
            return json(['code' => 500, 'msg' => '失败！']);
        }
	}
	//订单详情
    public function orderDetail(){
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 501, 'msg' => '详情id呢？']);
        }
        $res=Db::name('ordersub')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->field('a.*,b.img')->where('a.id',$id)->find();
        if($res){
        	$res['time']=date('Y-m-d H:i:s',$res['time']);
        	if ($res['fktime']) {
        		$res['fktime']=date('Y-m-d H:i:s',$res['fktime']);
        	}
        	if ($res['fhtime']) {
        		$res['fhtime']=date('Y-m-d H:i:s',$res['fhtime']);
        	}
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }

	//订单详情
    public function orderDetails(){
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 501, 'msg' => '详情id呢？']);
        }
        $res=Db::name('orders')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->field('a.*,b.img,b.title')->where('a.id',$id)->find();
        if($res){
        	$res['ordersubarr']=Db::name('ordersub')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->field('a.*,b.img')->where('a.orders_id',$res['id'])->order('a.id desc')->select();
        	$res['time']=date('Y-m-d H:i:s',$res['time']);
        	if ($res['fktime']) {
        		$res['fktime']=date('Y-m-d H:i:s',$res['fktime']);
        	}
        	if ($res['fhtime']) {
        		$res['fhtime']=date('Y-m-d H:i:s',$res['fhtime']);
        	}
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }
  	
    public function applyBack(){
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
        	$data['order_id']=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '订单id呢？']);
        }
        if(!empty(input("param.reason"))){
            $data['reason']=input("param.reason");
        }else{
            return json(['code' => 503, 'msg' => '原因呢？']);
        }
        if(!empty(input("param.message"))){
            $data['message']=input("param.message");
        }else{
            return json(['code' => 504, 'msg' => '留言说明呢？']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
		$data['user_id']=$uid;
		$data['state']=1;
		$data['time']=time();
		$imgs=[];
        $imgpath="../public/uploads/images/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath)){
            mkdir($imgpath,0777);
        }
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777);
        }
        chmod($imgpath.$imgsubpath,0777);
        if(isset($_FILES['imgs'])){
            for ($i=0; $i <count($_FILES['imgs']['name']) ; $i++) {
                move_uploaded_file($_FILES['imgs']['tmp_name'][$i],$imgpath.$imgsubpath.$_FILES['imgs']['name'][$i]);
                $imgs[$i]="/uploads/images/".$imgsubpath.$_FILES['imgs']['name'][$i];
            }
        }
        $data['imgs']=implode(",",$imgs);
        Db::startTrans();
        try {
			$res=Db::name('apply_back')->insertGetId($data);
    		Db::name('ordersub')->where('id',input("param.id"))->where('user_id',$uid)->update(['state'=>5,'uptime'=>time()]);
			// 提交事务
            Db::commit();
            return json(['code' => 200, 'msg' => '成功！']);
		} catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 500, 'msg' => '事物回滚失败！']);
        }
    }
    public function notify()
    {
    	$input = file_get_contents('php://input');
        $pay_result = simplexml_load_string($input, 'SimpleXMLElement',LIBXML_NOCDATA);
        if($pay_result->result_code=='SUCCESS'&&$pay_result->return_code=='SUCCESS'){
            $orders_id=Db::name('orders')->where('orderno',$pay_result->out_trade_no)->find();
            if($orders_id){
                if($orders_id['state']==1){
                    Db::name('orders')->where('orderno',$pay_result->out_trade_no)->update(['state'=>2,'uptime'=>time(),'fktime'=>time()]);
                    Db::name('ordersub')->where('orders_id',$orders_id['id'])->update(['state'=>2,'uptime'=>time(),'fktime'=>time()]);
                    //挣积分
		            if($orders_id['score']>0){
		        		Db::name('users')->where('id',$orders_id['user_id'])->setInc('score',input("param.score"));
			            $datascore['user_id']=$orders_id['user_id'];
			            $datascore['p_id']=$orders_id['p_id'];
			            $datascore['score']=$orders_id['score'];
			            $datascore['state']=1;
			            $datascore['type']=2;
			            $datascore['time']=time();
			            Db::name('score')->insertGetId($datascore);
					}
                }
            }else{
                $orders_id=Db::name('ordersub')->where('ordernosub',$pay_result->out_trade_no)->find();
                if($orders_id['state']==1){
                    Db::name('ordersub')->where('ordernosub',$pay_result->out_trade_no)->update(['state'=>2,'uptime'=>time(),'fktime'=>time()]);
                    //挣佣金
                    if($orders_id['score']>0){
		        		Db::name('users')->where('id',$orders_id['user_id'])->setInc('score',input("param.score"));
			            $datascore['user_id']=$orders_id['user_id'];
			            $datascore['p_id']=$orders_id['p_id'];
			            $datascore['score']=$orders_id['score'];
			            $datascore['state']=1;
			            $datascore['type']=2;
			            $datascore['time']=time();
			            Db::name('score')->insertGetId($datascore);
					}
                }
            }
        }
    }
    public function orderFinish()
    {
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
        	$id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '订单id呢？']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
		$data['state']=4;
		$data['uptime']=time();
    	$res=Db::name('ordersub')->where('id',$id)->where('user_id',$uid)->update($data);
		if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 500, 'msg' => '失败']);
        }
    }
    public function backDetail()
    {
    	if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
        	$id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '订单id呢？']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('ordersub')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->join('apply_back c', 'a.id = c.order_id','LEFT')->field('a.*,b.img,c.reason,c.message,c.state as back_state,c.time as apply_time,c.id as apply_id')->where('a.id',$id)->where('a.user_id',$uid)->where('a.state',5)->find();
        if($res){
          	$res['time']=date('Y-m-d H:i:s',$res['time']);
        	if ($res['fktime']) {
        		$res['fktime']=date('Y-m-d H:i:s',$res['fktime']);
        	}
        	if ($res['fhtime']) {
        		$res['fhtime']=date('Y-m-d H:i:s',$res['fhtime']);
        	}
        	if ($res['apply_time']) {
        		$res['apply_time']=date('Y-m-d H:i:s',$res['apply_time']);
        	}
            return json(['code' => 200, 'msg' => '成功','res'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '失败']);
        }
    }
  	public function backApply()
    {
    	if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
        	$id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '订单id呢？']);
        }
        if(!empty(input("param.apply_id"))){
        	$apply_id=input("param.apply_id");
        }else{
            return json(['code' => 503, 'msg' => '退款id呢？']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        Db::startTrans();
        try {
				Db::name('apply_back')->where('user_id',$uid)->where('state',1)->where('id',$apply_id)->update(['state'=>5]);
    			Db::name('ordersub')->where('id',$id)->where('user_id',$uid)->where('state',5)->update(['state'=>2,'uptime'=>time()]);
			// 提交事务
            Db::commit();
            return json(['code' => 200, 'msg' => '成功！']);
		} catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 500, 'msg' => '事物回滚失败！']);
        }
    }
  	public function backExpress()
    {
    	if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
        	$id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '订单id呢？']);
        }
        if(!empty(input("param.apply_id"))){
        	$apply_id=input("param.apply_id");
        }else{
            return json(['code' => 503, 'msg' => '退款id呢？']);
        }
        if(!empty(input("param.express_company"))){
        	$data['express_company']=input("param.express_company");
        }else{
            return json(['code' => 504, 'msg' => '快递公司？']);
        }
        if(!empty(input("param.express_no"))){
        	$data['express_no']=input("param.express_no");
        }else{
            return json(['code' => 505, 'msg' => '快递单号?']);
        }
        $data['state']=3;
        $uid=UserCrypt::do_mdecrypt($token);
		$res=Db::name('apply_back')->where('order_id',$id)->where('user_id',$uid)->where('state',2)->where('id',$apply_id)->update($data);
		if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 500, 'msg' => '失败']);
        }
    }
}